package com.lyj.service.impl;

import java.util.Date;
import java.util.List;

import com.lyj.dao.OptionDao;
import com.lyj.dao.RecordDao;
import com.lyj.dao.SubjectDao;
import com.lyj.dao.UserDao;
import com.lyj.dao.impl.OptionDaoImpl;
import com.lyj.dao.impl.RecordDaoImpl;
import com.lyj.dao.impl.SubjectDaoImpl;
import com.lyj.dao.impl.UserDaoImpl;
import com.lyj.pojo.Option;
import com.lyj.pojo.OptionQueryModel;
import com.lyj.pojo.Record;
import com.lyj.pojo.RecordQueryModel;
import com.lyj.pojo.Subject;
import com.lyj.pojo.SubjectQueryModel;
import com.lyj.pojo.User;
import com.lyj.service.SubjectService;
import com.lyj.util.RuleException;

/**
* 
* @author 李艺江
* @version 创建时间：2018年12月12日 上午11:16:32
*/

public class SubjectServiceImpl implements SubjectService{

	private UserDao userDao;
	private SubjectDao subjectDao;
	private OptionDao optionDao;
	private RecordDao recordDao;
	
	public SubjectServiceImpl() {
		userDao = new UserDaoImpl();
		subjectDao = new SubjectDaoImpl();
		optionDao = new OptionDaoImpl();
		recordDao = new RecordDaoImpl();
	}


	@Override
	public void add(Subject subject,User user) throws Exception {

		if(subject.getTitle().trim().equals("") || subject.getNumber() == 0) {
			throw new RuleException("不允许有空值!");
		}
		for(Option option:subject.getOptions()) {
			if(option.getContent().trim().equals("")) {
				throw new RuleException("不允许有空值!");
			}
		}
		
		//各个选项的内容不一致
		for(int i=0;i<subject.getOptions().size();i++){
			Option first = (Option)subject.getOptions().get(i);
			for(int j=i+1;j<subject.getOptions().size();j++){
				Option next = (Option)subject.getOptions().get(j);
				if(first.getContent().equals(next.getContent())){
					throw new RuleException("每个选项的内容不能一致");
				}
			}
		}
		
		//同一个发起人不能发起同样主题的投票项目
		SubjectQueryModel queryModel=new SubjectQueryModel();
		queryModel.setTitle(subject.getTitle());
		queryModel.getUser().setId(user.getId());
		List list = subjectDao.findByCondition(queryModel);
		if(list!=null && list.size()>0){
			throw new RuleException("同一个发起人不能发起同样主题的投票项目");
		}
		
		Long now = new Date().getTime();
		subject.setStartTime(now);
		subject.setEndTime(now + (24 * 60 * 60 * 1000));
		subject.setUser(user);
		
		subjectDao.insert(subject);
		subject.setId(subjectDao.findId());
		
		int i = 1;
		for(Option option:subject.getOptions()) {
			option.setIdx(i++);
			option.setSubjectId(subject.getId());
			
			optionDao.insert(option);
		}
	}


	@Override
	public List<Subject> getSubjects() throws Exception {
		List<Subject> subjects = subjectDao.findAll();
		for(Subject subject:subjects) {
			subject.setUser((User) userDao.findOne(subject.getUser().getId()));
			OptionQueryModel qm = new OptionQueryModel();
			qm.setSubjectId(subject.getId());
			List<Option>options = optionDao.findByCondition(qm);
			subject.setOptions(options);
			subject.setOptionSum();
			subject.setUserSum(subjectDao.getUserCount(subject.getId()));
		}
		return subjects;
	}


	@Override
	public Subject getSubject(int id) throws Exception {
		Subject subject = (Subject) subjectDao.findOne(id);
		
		subject.setUser((User) userDao.findOne(subject.getUser().getId()));
		
		OptionQueryModel qm = new OptionQueryModel();
		qm.setSubjectId(subject.getId());
		List<Option> options = optionDao.findByCondition(qm);
		for(Option option:options) {
			RecordQueryModel rm = new RecordQueryModel();
			rm.setOptionId(option.getId());
			List<Record> records = recordDao.findByCondition(rm);
			option.setRecords(records);
		}
		subject.setOptions(options);
		subject.setOptionSum();
		subject.setUserSum(subjectDao.getUserCount(subject.getId()));
	
		return subject;
	}


	@Override
	public void modify(Subject subject, User user) throws Exception {
		//已经存在投票记录，不允许进行修改
		if(subjectDao.getUserCount(subject.getId())>0){
			throw new Exception("已经存在投票记录，不可以修改");
		}
		//开始修改
		subjectDao.update(subject);
		optionDao.deleteOptions(subject.getId());
		for(int i=0;i<subject.getOptions().size();i++){
			Option op = (Option)subject.getOptions().get(i);
			op.setIdx(i+1);
			op.setSubjectId(subject.getId());
			optionDao.insert(op);
		}
	}
	
	
}
